#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}
bool _stmer;

const int N = 1e3 + 5;

int mod;

void Mod(int &x) {
    if (x >= mod) x -= mod;
    if (x < 0) x += mod;
}

class Matrix {

public:

    int A[N][N];
    int n, m;

    int* operator [](int x) { return A[x]; }

    Matrix () : A(), n(), m() {}

    Matrix (int _n, int _m) {
        n = _n, m = _m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                A[i][j] = 0;
    }

    Matrix (int _n, int _m, int flg) {
        n = _n, m = _m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                A[i][j] = 0;
        for (int i = 1; i <= n; i++)
            A[i][i] = 1;
    }

    friend Matrix operator *(Matrix x, Matrix y) {
        Matrix ans(x.n, y.m);
        for (int i = 1; i <= x.n; i++)
            for (int k = 1; k <= x.m; k++)
                for (int j = 1; j <= y.m; j++)
                    ans[i][j] += 1ll * x[i][k] * y[k][j] % mod, Mod(ans[i][j]);
        return ans;
    }

    friend bool operator ==(Matrix x, Matrix y) {
        bool ans = (x.n == y.n && x.m == y.m);
        for (int i = 1; i <= x.n; i++)
            for (int j = 1; j <= x.m; j++)
                ans &= x[i][j] == y[i][j];
        return ans;
    }

    void input() {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                A[i][j] = read();
    }
};

array <int, N> idx, isl;

void solve() {
    mod = read(); int n = read();
    idx[0] = 131;
    for (int i = 1; i <= 1e3; i++)
        idx[i] = idx[i - 1] * 131ll % mod, isl[i] = 0;
    Matrix A(n, n), B(n, n), C(n, n);
    A.input(), B.input(), C.input();
    /*
    if (n <= 300) {
        if (A * B == C) puts("Yes");
        else puts("No");
        return;
    }
    */
    bool flg = 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            isl[i] += 1ll * idx[j] * B[i][j] % mod, Mod(isl[i]);
    for (int i = 1; i <= n; i++) {
        int tp1 = 0, tp2 = 0;
        for (int j = 1; j <= n; j++) {
            tp1 += 1ll * idx[j] * C[i][j] % mod, Mod(tp1);
            tp2 += 1ll * A[i][j] * isl[j] % mod, Mod(tp2);
        }
        flg &= (tp1 == tp2);
    }
    isl.fill(0);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            isl[i] += 1ll * idx[j] * A[j][i] % mod, Mod(isl[i]);
    for (int i = 1; i <= n; i++) {
        int tp1 = 0, tp2 = 0;
        for (int j = 1; j <= n; j++) {
            tp1 += 1ll * idx[j] * C[j][i] % mod, Mod(tp1);
            tp2 += 1ll * B[j][i] * isl[j] % mod, Mod(tp2);
        }
        flg &= (tp1 == tp2);
    }
    if (flg) puts("Yes");
    else puts("No");
}

bool _edmer;
int main() {
    cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
#ifndef cxqghzj
    freopen("matrix.in", "r", stdin);
    freopen("matrix.out", "w", stdout);
#endif
    int T = read();
    while (T--) solve();
    return 0;
}
